/**
 * 右下角短消息提醒控件
 */

var __MSGERS = [];

//页面载入初始化
function get_msger()
{
	var msger = null;

	for( var i = 0; i < __MSGERS.length; i++ )
	{
		// 搜索一个已经创建，但未使用的消息提示器
		if( !__MSGERS[ i ].visible() )
		{
			msger = __MSGERS[ i ];
			break;
		}
	}

	if( msger == null )
	{
		msger = new MessageSystem();
		msger.init();
		__MSGERS.push( msger );
	}
	return msger;
}

//窗口大小改变
$( window ).resize( function( e )
{
	var msger = null;

	for( var i = 0; i < __MSGERS.length; i++ )
	{
		msger = __MSGERS[ i ];

		// 对所有显示的消息提示器根据当前窗口重新调整位置
		if( msger.visible() )
		{
			msger.relocate();
		}
	}
} );

function MessageSystem()
{
	var html = "<div id='{id}' class='__DIV_BORDER' style='position:absolute;z-index:9999;width:450px;height:200px;'>"
			+ "<dt><span id='title'>消息提醒</span><a id='btn_close'>[ X ]</a></dt>"
			+ "<div><ul id='body'></ul></div>" + "</div>";

	var $box;
	var $title;
	var $body;

	this.init = function()
	{
		var id = "__DIV_MESSAGE_SYSTEM_" + hash( 5 );
		html = html.replace( /\{id\}/ig, id );
		$( "body" ).append( html );
		$box = $( "#" + id );
		$title = $box.find( "#title" );
		$body = $box.find( "#body" );

		$box.find( "#btn_close" ).click( { msg: this }, function( e )
		{
			e.data.msg.close();
		} );

		$box.hide();
	};

	// 是否显示
	this.visible = function()
	{
		return $box.visible();
	};

	// 显示单条信息
	this.open = function( title )
	{
		!empty( title ) && $title.html( title );
		$box.move( $( "body" ).width() - $box.width() - 15, $( "body" ).height() );
		$box.animate( { top: $( "body" ).height() - $box.height() - 15, opacity: 'show' }, 300 );
	};

	this.relocate = function()
	{
		var bw = $( "body" ).width();
		var bh = $( "body" ).height();
		var w = $box.width();
		var h = $box.height();

		$box.move( bw - w - 15, bh - h - 15 );
	};

	this.close = function()
	{
		$box.animate( { top: $( "body" ).height() + 10, left: $( "body" ).width() + 10, opacity: 'hide' },
				100 );
	};

	//--------------------------------------------------------------------------
	//
	//	消息管理
	//
	//--------------------------------------------------------------------------

	/**
	 * 追加消息提醒
	 */
	this.msg = function( content, func, params )
	{
		$body.append( "<li><input type='checkbox' title='勾选后不再提示'/>" + content + "</li>" );

		var li = $body.find( "li:last" );
		params = empty( params ) ? {} : params;
		params.self = li;
		typeof ( func ) == "function" && li.click( params, func );
		li.find( "input:checkbox" ).click( function( e )
		{
			e.stopPropagation();
		} );
		li.click( { msg: this }, function( e )
		{
			li.remove();
			e.data.msg.adjust();
		} );
		this.adjust();
	};

	/**
	 * 清除所有消息提醒
	 */
	this.clear = function()
	{
		$body.empty();
	};

	/**
	 * 根据当前消息数量调整消息框的高度
	 */
	this.adjust = function()
	{
		var size = $body.find( "li" ).length;
		var h = size * 25 + 45;

		if( size == 0 )
		{
			this.close();
		}

		$box.height( h );
	};
}
